﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using XamlingCore.SL.Controls.Content;
using XamlingCore.SL.Controls.ContentTemplate;
using XamlingCore.SL.Controls.ContentTemplate.Interfaces;

namespace XamlingCore.SL.Controls.Navigation
{
    [StyleTypedProperty(Property = "TransitionStyle", StyleTargetType = typeof(XTransitionContentControl))]
    public class XNavigationFrame : Control
    {
        private XTransitionContentControl transControl;

        public XNavigationFrame()
        {
            //default to converntion based template selection, can override by setting this value
            ContentTemplateSelector = new ContentTemplateSelectorBase();

            DefaultStyleKey = typeof (XNavigationFrame);
        }

        protected void SetTransitionStyle(Style s)
        {
            if (TransControl != null) TransControl.Style = s;
        }

        public override void OnApplyTemplate()
        {
            TransControl = GetTemplateChild("TransControl") as XTransitionContentControl;
        }

        private void SetTemplateSelector(IContentTemplateSelector i)
        {
            if (TransControl != null) TransControl.TemplateSelector = i;
        }

        public IContentTemplateSelector ContentTemplateSelector
        {
            get { return (IContentTemplateSelector)GetValue(ContentTemplateSelectorProperty); }
            set { SetValue(ContentTemplateSelectorProperty, value); }
        }

        // Using a DependencyProperty as the backing store for ContentTemplateSelector.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty ContentTemplateSelectorProperty =
            DependencyProperty.Register("ContentTemplateSelector", typeof(IContentTemplateSelector), typeof(XNavigationFrame), new PropertyMetadata(null, setTemplateSelector));

        private static void setTemplateSelector(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var obj = d as XNavigationFrame;
            obj.SetTemplateSelector(e.NewValue as IContentTemplateSelector);
        }

        public Style TransitionStyle
        {
            get { return (Style)GetValue(TransitionStyleProperty); }
            set { SetValue(TransitionStyleProperty, value); }
        }

        public XTransitionContentControl TransControl
        {
            get { return transControl; }
            set
            {
                transControl = value;
                if (transControl != null)
                    if (ContentTemplateSelector != null) transControl.TemplateSelector = ContentTemplateSelector;
            }
        }

        // Using a DependencyProperty as the backing store for TransitionStyle.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TransitionStyleProperty =
            DependencyProperty.Register("TransitionStyle", typeof(Style), typeof(XNavigationFrame), new PropertyMetadata(null, styleSet));

        private static void styleSet(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var obj = d as XNavigationFrame;
            obj.SetTransitionStyle(e.NewValue as Style);
        }
    }
}
